package cn.com.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import net.sf.jxls.reader.ReaderBuilder;
import net.sf.jxls.reader.XLSReadStatus;
import net.sf.jxls.reader.XLSReader;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.xml.sax.SAXException;
import cn.com.view.MainFrame;
import db.JDBConnection;
import domain.Gdzc;
import domain.JxlsUtile;
/**
 * 按钮事件
 * @author Administrator
 */

public class BoolBarAction implements ActionListener {
	
	private MainFrame frame;
	
	private String p;
	
	private File f;
	
	private static int index = 0;

	public BoolBarAction(MainFrame frame) {
		this.frame = frame;
	}
	
	public void approveSelection(ActionEvent e){
		String title = e.getActionCommand();
		if("保存".equals(title)){
			
		}
	}
	/**
	 * 单击按钮执行相应的方法
	 */
	public void actionPerformed(ActionEvent e) {
 		String title = e.getActionCommand();
		if ("选择".equals(title)) {
			JFileChooser  fd = frame.getFd();
			int result=fd.showOpenDialog(null);
			if(result == JFileChooser.APPROVE_OPTION){
				 f = fd.getSelectedFile(); 
				if(f != null){
					String path=f.getPath();
					//p=path.replace("\\", "\\\\");
					frame.getPath().setText(path);
				}
			}
		}else if("导入".equals(title) && p != ""){
			String acc=(String) frame.getAcc().getSelectedItem();
			if("".equals(acc) || acc == null){
				JOptionPane.showMessageDialog(frame.getContentPane(), "账套号不能为空", "提示", JOptionPane.ERROR_MESSAGE);
			return;
			}
			String accName=frame.getAccname().getText().trim();
			String year=frame.getYear().getText().trim();
			if("".equals(acc) || acc == null){
				JOptionPane.showMessageDialog(frame.getContentPane(), "年度不能为空", "提示", JOptionPane.ERROR_MESSAGE);
			return;
			}
			//year=year.replace("-", "");
			String type=frame.getImptype().getSelectedItem().toString().trim();
			if(f == null){
				JOptionPane.showMessageDialog(frame.getContentPane(), "请选择路径", "提示", JOptionPane.ERROR_MESSAGE);
			return;
			}
			List gdzcs = JxlsUtile.getJxls().readData(f);
			String dataName = "USER"+acc+year;
			gdzcs.remove(0);
			
			if(gdzcs.size() == 0 || gdzcs == null){
				return;
			}
			String dm = "";
			/**
			 * for (int i = 0; i < gdzcs.size(); i++) {
				Gdzc gdzc = (Gdzc) gdzcs.get(i);
				dm = dm + gdzc.getDm() + ",";
			    }
			    
			    dm = dm.substring(0,dm.length()-1);
			 */
			Gdzc gdzc = (Gdzc) gdzcs.get(0);
			dm = gdzc.getDm();
			
			JDBConnection sqlServer = JDBConnection.SqlServer();
			
			if("增加".equals(type)){
				int count = 0;
			    sqlServer = JDBConnection.SqlServer();
			    //gd_kf
			    String sql = "select count(*) from gd_kp where dm in("+dm+")";
				System.out.println(sql);
				int result=sqlServer.isNo(sql, dataName);
				if(index == 1){
					return;
				}
				if(result > 0){
					JOptionPane.showMessageDialog(frame.getContentPane(), "资产代码已在，不能重复导入", "提示", JOptionPane.ERROR_MESSAGE);
				}else{
					count=sqlServer.insterDate(gdzcs,dataName); //导入gd_kp
					count=sqlServer.insterGdmx(gdzcs,type,dataName); //导入gd_mx
				}
				
				 //gd_mx
//			    String sql2 = "select count(*) from gd_mx where dm in("+dm+")";
//				System.out.println(sql2);
//				int mxcode=sqlServer.isNo(sql2, dataName);
//				if(mxcode > 0){
//					JOptionPane.showMessageDialog(frame.getContentPane(), "资产代码已在，不能重复导入", "提示", JOptionPane.ERROR_MESSAGE);
//				}else{
//					count=sqlServer.insterGdmx(gdzcs,type,dataName); //导入gd_mx
//				}
			if (count > 0) {
				JOptionPane.showMessageDialog(frame.getContentPane(), "导入成功", "提示",JOptionPane.INFORMATION_MESSAGE);
			}
			}else if("减少".equals(type)){
				//String sql = "select count(*) from gd_mx where dm in("+dm+")";
				//System.out.println(sql);
				//int result=sqlServer.isNo(sql, dataName);
				//if(result > 0){
				//	JOptionPane.showMessageDialog(frame.getContentPane(), "资产代码已在，不能重复导入", "提示", JOptionPane.ERROR_MESSAGE);
				//}else{
					sqlServer = JDBConnection.SqlServer();
					int cou=sqlServer.insterGdmx(gdzcs, type,dataName);
					if (cou > 0) {
						JOptionPane.showMessageDialog(frame.getContentPane(), "导入成功", "提示",JOptionPane.INFORMATION_MESSAGE);
					}
				//}
			}
	    } else if ("退出".equals(title)) {
			System.exit(0); 
	    }

	}

	
	public static int getIndex() {
		return index;
	}

	
	public static void setIndex(int index) {
		BoolBarAction.index = index;
	}

}
